--
-- Author: ping.wang
-- Date: 2018-07-02 10:59:55
--
-- NewguildTalentIconRender
--

local ClassRef = app.mvc.createRenderClass(...)

--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.NewGuildTalentIconNode))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

--
function ClassRef:init(data)
    self.super.init(self, data)
    self:setContentSize(self._ui.Panel:getContentSize())
--[[
    self._sensImg = self._ui.sens --icon可以被激活的发光图
    self._light = self._ui.light --icon已经被激活后显示的图
    self._selected = self._ui.selected -- icon的选中图 表面是否选中
    self._icon = self._ui.icon --icon 本体图 tanel skill的图标
    self._mask = self._ui.mask --icon未被激活的时候显示一个灰色遮罩
    self._lock = self._ui.lock --icon激活但是贡献等级不足的时候显示的一个锁
    self._icon_add = self._ui.icon_add --icon未被激活但是可以激活的时候显示一个+号
    self._skillLv  = self._ui.num --天赋技能等级 默认都显示1级先
    self._skillLv:setString("1")
--]]
    self._ui.lv_num:setVisible(false)
    self._ui.lock:setVisible(false)
    self._ui.light:setVisible(false)
    self._ui.mask:setVisible(false)
    self._ui.sens:setVisible(false)
    self._ui.Panel:setSwallowTouches(false)
    self:setClickEnable(true)
    display.uiAddClick(self._ui.Panel, function(sender, eventType)
        if not self:isSelected() then
            self:setSelected(true)
        end
        executeFunc(self._onClick, self)
    end)
    self:addTouchEvent()
end

function ClassRef:refreshWithData(data)
    self._data = data
    if self._data then
        self:setVisible(true)
        self:setIcon(data:getIconName())
        self:setActive(data:isActive())
        if not data:isActive() then
            self:setCanLearn(data:canGet())
        else
            self:setCanLearn(false)
        end

        local needDonateLv = data:getNeedDonateLv()
        if needDonateLv > 1 then
            self._ui.lv_num:setVisible(true)
            self._ui.num:setString(tostring(needDonateLv))
        else
            self._ui.lv_num:setVisible(false)
        end
    end
end

function ClassRef:refreshCanLearn()
    if self._data then
        if not data.active then
            self:setCanLearn(data:canGet())
        else
            self:setCanLearn(false)
        end
    end
end

function ClassRef:getItemName()
    return self._data:getTalentName()
end

function ClassRef:getItemDesc()
    return self._data:getTalentDes()
end


function ClassRef:setIcon(res)
    -- print("TalentRender:setIcon:",res)
    self._ui.icon:setVisible(true)
    self._ui.icon:loadTexture(res)
end

function ClassRef:setActive(a)
    self._ui.light:setVisible(a)
    self._ui.mask:setVisible(not a)
end

function ClassRef:setLockVisible(a)
    self._ui.lock:setVisible(a)
end

function ClassRef:setSelected(s)
    self._ui.selected:setVisible(s)
end

function ClassRef:isSelected()
    return self._ui.selected:isVisible()
end

function ClassRef:setCanLearn(b)
    self._ui.sens:setVisible(b)
    self._ui.icon_add:setVisible(b)
end

function ClassRef:setClickEnable(b)
    self._ui.Panel:setTouchEnabled(b)
end

function ClassRef:getData()
    return self._data
end


function ClassRef:addTouchEvent()
    local function touchEvent(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            executeFunc(self._onClick, self)
        end
    end
    self._ui.Panel:addTouchEventListener(touchEvent)
end

function ClassRef:setClickHandler(cb)
    self._onClick = cb
end

return ClassRef
